#include "globals.h"
#include "Frustum.h"

// A structure for our custom vertex type
struct tri_vertex
{
    FLOAT x, y, z;      // The transformed position for the vertex
    DWORD color;        // The vertex color
};

const DWORD tri_fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE;


void Frustum::Draw() 
{
	g_engine.getVCard()->SetStreamSource( 0, g_pVB, 0, sizeof( tri_vertex ) );
	g_engine.getVCard()->SetFVF( tri_fvf );
	g_engine.getVCard()->DrawPrimitive( D3DPT_TRIANGLELIST, 12, 16 );
	//g_engine.getVCard()->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 4 );
}

HRESULT Frustum::Init() 
{
	tri_vertex data[]= {
   //Pyramid vertices
   {-1.0f,-1.0f,-1.0f,0x0000FF00},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{ 1.0f,-1.0f,-1.0f,0x000000FF},
   { 1.0f,-1.0f,-1.0f,0x000000FF},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{ 1.0f,-1.0f, 1.0f,0x0000FF00},
   { 1.0f,-1.0f, 1.0f,0x0000FF00},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{-1.0f,-1.0f, 1.0f,0x000000FF},
   {-1.0f,-1.0f, 1.0f,0x000000FF},{ 0.0f, 1.0f, 0.0f,0x00FF0000},{-1.0f,-1.0f,-1.0f,0x0000FF00},
   //Cube vertices
      //Front face
   {-1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f, 1.0f,-1.0f,0xFF0000FF},
   { 1.0f, 1.0f,-1.0f,0xFF0000FF},{ 1.0f,-1.0f,-1.0f,0xFF0000FF},{-1.0f,-1.0f,-1.0f,0xFF0000FF},
      //Back face
   { 1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f, 1.0f, 1.0f,0xFF0000FF},
   {-1.0f, 1.0f, 1.0f,0xFF0000FF},{-1.0f,-1.0f, 1.0f,0xFF0000FF},{ 1.0f,-1.0f, 1.0f,0xFF0000FF},
      //Top face
   {-1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f, 1.0f,0xFFFF0000},
   { 1.0f, 1.0f, 1.0f,0xFFFF0000},{ 1.0f, 1.0f,-1.0f,0xFFFF0000},{-1.0f, 1.0f,-1.0f,0xFFFF0000},
      //Bottom face
   { 1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f, 1.0f,0xFFFF0000},
   {-1.0f,-1.0f, 1.0f,0xFFFF0000},{-1.0f,-1.0f,-1.0f,0xFFFF0000},{ 1.0f,-1.0f,-1.0f,0xFFFF0000},
      //Left face
   {-1.0f,-1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f, 1.0f,0xFF00FF00},{-1.0f, 1.0f,-1.0f,0xFF00FF00},
   {-1.0f, 1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f,-1.0f,0xFF00FF00},{-1.0f,-1.0f, 1.0f,0xFF00FF00},
      //Right face
   { 1.0f,-1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f,-1.0f,0xFF00FF00},{ 1.0f, 1.0f, 1.0f,0xFF00FF00},
   { 1.0f, 1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f, 1.0f,0xFF00FF00},{ 1.0f,-1.0f,-1.0f,0xFF00FF00},
	};
	void *vb_vertices;
	HRESULT hr;

	hr = g_engine.getVCard()->CreateVertexBuffer( sizeof(data),      //Length
												  D3DUSAGE_WRITEONLY,//Usage
												  tri_fvf,           //FVF
												  D3DPOOL_MANAGED,   //Pool
												  &g_pVB,        //ppVertexBuffer
												  NULL);             //Handle
	if(FAILED(hr))
	{
	  return hr;
	}

	hr=g_pVB->Lock(0, //Offset
					  0, //SizeToLock
					  &vb_vertices, //Vertices
					  0);  //Flags
	if(FAILED(hr))
	{
	  return hr;
	}

	memcpy( vb_vertices, data,sizeof(data));

	g_pVB->Unlock();


	return D3D_OK;
}

void Frustum::getWorldMatrix(D3DXMATRIXA16 *matWorld) 
{
	// Set up the rotation matrix to generate 1 full rotation (2*PI radians) 
    // every 1000 ms. To avoid the loss of precision inherent in very high 
    // floating point numbers, the system time is modulated by the rotation 
    // period before conversion to a radian angle.
    UINT iTime = timeGetTime() % 1000;
    FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f;
    D3DXMatrixRotationY( matWorld, fAngle );
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Frustum::Cleanup()
{
    if( g_pVB != NULL )
        g_pVB->Release();
}